package com.example.controller;

import com.example.common.Result;
import com.example.entity.CourseCatalog;
import com.example.service.CourseCatalogService;
import org.springframework.web.bind.annotation.RestController; // 用于标注控制器组件
import org.springframework.web.bind.annotation.GetMapping; // 用于标注GET请求映射
import org.springframework.web.bind.annotation.PathVariable; // 用于标注路径变量
import org.springframework.web.bind.annotation.RequestMapping; // 用于标注请求映射的路径
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.ResponseEntity; // 用于构造响应体
import org.springframework.beans.factory.annotation.Autowired; // 用于自动装配

import java.util.List;

@RestController
@RequestMapping("/courseCatalog")
public class CourseCatalogController {
    @Autowired
    private CourseCatalogService courseCatalogService;

    @GetMapping("/getByCourseId/{courseId}")
    public ResponseEntity<List<CourseCatalog>> getByCourseId(@PathVariable Long courseId) {
        List<CourseCatalog> courseCatalog = courseCatalogService.findAllByCourseId(courseId);
        return ResponseEntity.ok(courseCatalog);
    }

    // 添加用于根据ID获取课程目录详情的方法
    @GetMapping("/selectById1/{id}")
    public ResponseEntity<CourseCatalog> selectById1(@PathVariable Long id) {
        CourseCatalog courseCatalog = courseCatalogService.findById(id);
        if (courseCatalog != null) {
            return ResponseEntity.ok(courseCatalog);
        } else {
            return ResponseEntity.status(404).body(null); // 如果找不到，返回404
        }
    }




    @PostMapping("/add")
    public Result addCourseCatalog(@RequestBody CourseCatalog courseCatalog) {
        try {
            courseCatalogService.addCourseCatalog(courseCatalog);
            return Result.success();
        } catch (Exception e) {
            return Result.error("500", e.getMessage());
//            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
        }
    }
    @PutMapping("/update/{id}")
    public ResponseEntity<Result> updateCourseCatalog(@PathVariable Long id, @RequestBody CourseCatalog courseCatalog) {
        if (courseCatalog == null || (courseCatalog.getId() != null && !id.equals(courseCatalog.getId()))) {
            // 请求参数错误
            return ResponseEntity.status(HttpStatus.BAD_REQUEST)
                    .body(Result.error("400", "ID不匹配或请求体中缺少CourseCatalog对象"));
        }
        try {
            courseCatalogService.updateCourseCatalog(id, courseCatalog);
            // 更新成功
            return ResponseEntity.ok(Result.success());
        } catch (Exception e) {
            // 系统错误
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Result.error("500", "更新失败: " + e.getMessage()));
        }
    }

    @DeleteMapping("/delete/{id}")
    public ResponseEntity<Result> deleteCourseCatalog(@PathVariable Long id) {
        try {
            courseCatalogService.deleteCourseCatalog(id);
            return ResponseEntity.ok(Result.success()); // 返回成功响应
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Result.error("500", "删除失败: " + e.getMessage()));
        }
    }
}